package com.jinlejia.erp.adapter

import android.content.Context
import android.graphics.Color
import android.text.Html
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.freedom.yefeng.yfrecyclerview.YfListAdapter
import com.freedom.yefeng.yfrecyclerview.YfSimpleViewHolder
import com.jinlejia.erp.R
import com.jinlejia.erp.databinding.ItemProcessFollwupBinding
import com.jinlejia.erp.utils.DateUtil

/**
 * @author SongJiaTai
 * @description:进程跟踪Adapter
 * @date :2020/1/14 15:51
 */
class ProcessTrackingAdapter constructor(data: ArrayList<HashMap<String, Any>>) :
    YfListAdapter<HashMap<String, Any>>(data) {
    private lateinit var mContext: Context
    private var listener: OnDetailsListener? = null

    override fun onCreateDataViewHolder(parent: ViewGroup?): RecyclerView.ViewHolder {
        mContext = parent!!.context.applicationContext
        var view = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_process_follwup, parent, false)
        return ViewHolder(view)
    }

    override fun onCreateEmptyViewHolder(parent: ViewGroup?): RecyclerView.ViewHolder {
        val view =
            LayoutInflater.from(parent!!.context).inflate(R.layout.empty_layout, parent, false)
        val txt = view.findViewById<TextView>(R.id.id_tips)
        txt.text = "暂无数据"
        return YfSimpleViewHolder(view)
    }

    override fun onBindDataViewHolder(holder: RecyclerView.ViewHolder?, position: Int) {
        val binding = DataBindingUtil.getBinding<ItemProcessFollwupBinding>(holder!!.itemView)
        val hashMap = data[position]

        //region 进程节点名称与时间
        if (hashMap["trackType"] == 1) {
            binding!!.tvNodeName.text = "经纪人推荐"
        } else if (hashMap["trackType"] == 2) {
            binding!!.tvNodeName.text = "经纪人带访"
        } else if (hashMap["trackType"] == 3) {
            binding!!.tvNodeName.text = "认购"
        } else if (hashMap["trackType"] == 4) {
            binding!!.tvNodeName.text = "预计签约时间"
        } else if (hashMap["trackType"] == 5) {
            binding!!.tvNodeName.text = "签约"
        } else if (hashMap["trackType"] == 6) {
            binding!!.tvNodeName.text = "预计回款时间"
        } else if (hashMap["trackType"] == 7) {
            binding!!.tvNodeName.text = "预计开票时间"
        } else if (hashMap["trackType"] == 8) {
            binding!!.tvNodeName.text = "是否垫付"
        } else if (hashMap["trackType"] == 9) {
            binding!!.tvNodeName.text = "全款到账"
        } else if (hashMap["trackType"] == 10) {
            binding!!.tvNodeName.text = "400回访"
        } else if (hashMap["trackType"] == 11) {
            binding!!.tvNodeName.text = "对账"
        } else if (hashMap["trackType"] == 12) {
            binding!!.tvNodeName.text = "申请开票"
        } else if (hashMap["trackType"] == 13) {
            binding!!.tvNodeName.text = "审核通过"
        } else if (hashMap["trackType"] == 14) {
            binding!!.tvNodeName.text = "案场领票"
        } else if (hashMap["trackType"] == 15) {
            binding!!.tvNodeName.text = "发票送达开发商"
        } else if (hashMap["trackType"] == 16) {
            binding!!.tvNodeName.text = "财务回款确认"
        } else if (hashMap["trackType"] == 17) {
            binding!!.tvNodeName.text = "案场提交结佣申请"
        } else if (hashMap["trackType"] == 18) {
            binding!!.tvNodeName.text = "结佣申请审核完成"
        } else if (hashMap["trackType"] == 19) {
            binding!!.tvNodeName.text = "财务确认付款"
        }

        if (hashMap.containsKey("trackTime")) {
            binding!!.tvNodeTime.text = DateUtil.getTime(hashMap["trackTime"].toString())
            binding.tvNodeTime.visibility = View.VISIBLE
        } else {
            binding!!.tvNodeTime.visibility = View.INVISIBLE
        }
        //endregion

        //region 是否完成isFinish
        if (position == data.size - 1) {
            if (hashMap["isFinish"] == 0) {
                binding.viewLine.visibility = View.GONE
                binding.vZw.visibility = View.GONE
                binding.tvNodeTime.visibility = View.GONE
                binding.tvNodeName.setTextColor(Color.parseColor("#999999"))
                binding.imgNode.setBackgroundResource(R.drawable.bg_process_grear)
            } else {
                binding.viewLine.visibility = View.GONE
                binding.vZw.visibility = View.GONE
                binding.tvNodeTime.visibility = View.VISIBLE
                binding.tvNodeName.setTextColor(Color.parseColor("#1B1B1B"))
                binding.imgNode.setBackgroundResource(R.drawable.bg_process_black)
            }
        } else {
            if (hashMap["isFinish"] == 0) {
                binding!!.vZw.visibility = View.GONE
                binding.tvNodeTime.visibility = View.INVISIBLE
                binding.tvNodeName.setTextColor(Color.parseColor("#999999"))
                binding.imgNode.setBackgroundResource(R.drawable.bg_process_grear)
            } else {
                binding!!.vZw.visibility = View.VISIBLE
                binding.tvNodeTime.visibility = View.VISIBLE
                binding.tvNodeName.setTextColor(Color.parseColor("#1B1B1B"))
                binding.imgNode.setBackgroundResource(R.drawable.bg_process_black)
            }
        }
        //endregion

        //region 详情按钮显示影藏
        if (hashMap["isFollow"] == 1) {
            binding.tvXq.visibility = View.VISIBLE
        } else {
            binding.tvXq.visibility = View.GONE
        }
        //endregion

        //region 是否回访是否垫付红色字体
        if (hashMap["trackTab"] == 1) {
            if (hashMap["yesNo"] == 1) {
                if (hashMap.containsKey("trackTime")) {
                    if (hashMap["isFinish"] != 0) {
                        val str =
                            "<font color=\"red\">" + "回访通过&nbsp&nbsp&nbsp&nbsp" + "</font>" + "\n\n" + DateUtil.getTime(
                                hashMap["trackTime"].toString()
                            )
                        binding!!.tvNodeTime.text = Html.fromHtml(str)
                        binding.tvNodeTime.visibility = View.VISIBLE
                    } else {
                        binding!!.tvNodeTime.text =
                            DateUtil.getTime(hashMap["trackTime"].toString())
                        binding.tvNodeTime.visibility = View.VISIBLE
                    }

                } else {
                    if (hashMap["isFinish"] != 0) {
                        val str = "<font color=\"red\">" + "回访通过&nbsp&nbsp&nbsp&nbsp" + "</font>"
                        binding!!.tvNodeTime.text = Html.fromHtml(str)
                        binding.tvNodeTime.visibility = View.VISIBLE
                    } else {
                        binding.tvNodeTime.visibility = View.INVISIBLE
                    }

                }
            } else if (hashMap["yesNo"] == 0) {
                if (hashMap.containsKey("trackTime")) {
                    if (hashMap["isFinish"] != 0) {
                        val str =
                            "<font color=\"red\">" + "回访未通过&nbsp&nbsp&nbsp&nbsp" + "</font>" + "\n\n" + DateUtil.getTime(
                                hashMap["trackTime"].toString()
                            )
                        binding!!.tvNodeTime.text = Html.fromHtml(str)
                        binding.tvNodeTime.visibility = View.VISIBLE
                    } else {
                        binding!!.tvNodeTime.text =
                            DateUtil.getTime(hashMap["trackTime"].toString())
                        binding.tvNodeTime.visibility = View.VISIBLE
                    }

                } else {
                    if (hashMap["isFinish"] != 0) {
                        val str = "<font color=\"red\">" + "回访未通过&nbsp&nbsp&nbsp&nbsp" + "</font>"
                        binding!!.tvNodeTime.text = Html.fromHtml(str)
                        binding.tvNodeTime.visibility = View.VISIBLE
                    } else {
                        binding.tvNodeTime.visibility = View.INVISIBLE
                    }
                }
            } else {
                if (hashMap.containsKey("trackTime")) {
                    if (hashMap["isFinish"] != 0) {
                        val str =
                            "<font color=\"red\">" + "无400回访&nbsp&nbsp&nbsp&nbsp" + "</font>" + "\n\n" + DateUtil.getTime(
                                hashMap["trackTime"].toString()
                            )
                        binding!!.tvNodeTime.text = Html.fromHtml(str)
                        binding.tvNodeTime.visibility = View.VISIBLE
                    } else {
                        binding!!.tvNodeTime.text =
                            DateUtil.getTime(hashMap["trackTime"].toString())
                        binding.tvNodeTime.visibility = View.VISIBLE
                    }
                } else {
                    if (hashMap["isFinish"] != 0) {
                        val str = "<font color=\"red\">" + "无400回访&nbsp&nbsp&nbsp&nbsp" + "</font>"
                        binding!!.tvNodeTime.text = Html.fromHtml(str)
                        binding.tvNodeTime.visibility = View.VISIBLE
                    } else {
                        binding.tvNodeTime.visibility = View.INVISIBLE
                    }

                }
            }
        } else if (hashMap["trackTab"] == 2) {
            if (hashMap["isAdvance"] == 1) {
                    if (hashMap["yesNo"] != 0) {
                        if (hashMap.containsKey("trackTime")) {
                            val str =
                                "<font color=\"red\">" + "垫付项目<br>" + "已垫付&nbsp&nbsp&nbsp&nbsp" + "</font>" + DateUtil.getTime(
                                    hashMap["trackTime"].toString()
                                )
                            binding!!.tvNodeTime.text = Html.fromHtml(str)
                            binding.tvNodeTime.visibility = View.VISIBLE
                        } else {
                            val str =
                                "<font color=\"red\">" + "垫付项目<br>" + "已垫付&nbsp&nbsp&nbsp&nbsp" + "</font>"
                            binding!!.tvNodeTime.text = Html.fromHtml(str)
                            binding.tvNodeTime.visibility = View.VISIBLE
                        }
                    } else {
                        if (hashMap.containsKey("trackTime")) {
                            val str =
                                "<font color=\"red\">" + "垫付项目<br>" + "未垫付&nbsp&nbsp&nbsp&nbsp" + "</font>" + DateUtil.getTime(
                                    hashMap["trackTime"].toString()
                                )
                            binding!!.tvNodeTime.text = Html.fromHtml(str)
                            binding.tvNodeTime.visibility = View.VISIBLE
                        } else {
                            val str =
                                "<font color=\"red\">" + "垫付项目<br>" + "未垫付&nbsp&nbsp&nbsp&nbsp" + "</font>"
                            binding!!.tvNodeTime.text = Html.fromHtml(str)
                            binding.tvNodeTime.visibility = View.VISIBLE
                        }
                    }
            } else {
                val str = "<font color=\"red\">" + "非垫付项目&nbsp&nbsp&nbsp&nbsp" + "</font>"
                binding!!.tvNodeTime.text = Html.fromHtml(str)
                binding.tvNodeTime.visibility = View.VISIBLE
            }
        }
//endregion

        binding.tvXq.setOnClickListener {
            listener!!.onClick(hashMap["id"].toString().toInt())
        }

    }


    private class ViewHolder constructor(view: View) : RecyclerView.ViewHolder(view) {

        init {
            DataBindingUtil.bind<ItemProcessFollwupBinding>(view)
        }
    }

    fun setDetailsListener(onDetailsListener: OnDetailsListener) {
        this.listener = onDetailsListener
    }

    interface OnDetailsListener {
        fun onClick(trackId: Int)
    }
}